package com.example.SpringCloudUpload.util;

/**
 * Class Description
 *
 * @author hulilei
 * @version 1.0.0
 * @date 2021/10/31 9:58
 */


import com.example.SpringCloudUpload.entity.Chunk;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

/**
 * 上传文件检查工具类
 */
@Component
public class FileCheckUtil {
    /**
     * 通过文件头判断文件类型
     * 对文件类型进行检查
     *
     * @param is       上传文件的输入流
     * @param uploadType 上传文件的类型（模型文件、数据集、功能说明书）
     */
    //public boolean checkFileType(File file, String uploadType) {
    public boolean checkFileType(InputStream is, String uploadType) {
        byte[] b = new byte[4];
        String suffix;
        try {
            // 截取输入流文件信息
            is.read(b, 0, b.length);
            // 获取文件头信息
            suffix = bytesToHexString(b);
            suffix = suffix.toUpperCase();
        } catch (IOException e) {
            System.out.println(e);
            return false;
        }
        if (uploadType.equals("modelFile")) {
            switch (suffix) {
                // ONNX文件
                case "0803120B":
                    return true;
                default:
                    return false;
            }
        } else if (uploadType.equals("specification")) {
            switch (suffix) {
                // PDF
                case "25504446":
                    return true;
                // DOC
                case "D0CF11E0":
                case "0D444F43":
                case "12345678":
                case "31BE0000":
                case "7FFE340A":
                    return true;
                default:
                    return false;
            }
        } else if (uploadType.equals("requirement")) {
            /**
            switch (suffix) {
                // requirement.txt
                // https://blog.csdn.net/jingliangliu/article/details/76718477
                case "EFBBBF": // 69707974
                    return true;
                default:
                    return false;
            }
             */
            /**
             * txt文件头不好做，暂时仅校对文件名后缀
             */
            return true;
        } else if (uploadType.equals("dataset")) {
            switch (suffix) {
                // zip
                case "504B0304":
                    return true;
                // rar
                case "52617221":
                    return true;
                // 7z
                // https://blog.csdn.net/u014608280/article/details/79556814?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.no_search_link&spm=1001.2101.3001.4242.1
                case "377ABCAF":
//                case "377ABCAF271C":
                    return true;
                default:
                    return false;
            }
        } else {
            System.out.println("param uploadType is not support.");
            return false;
        }
    }

    /**
     * 将byte数组转换为16进制的字符串
     */
    public String bytesToHexString(byte[] src) {
        // 构建新的字符串列
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        // 转为16进制
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 对文件名后缀进行检测
     */
    public boolean checkSuffix(String fileName, String uploadType) {
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        if(uploadType.equals("modelFile")) {
            switch (suffix) {
                case ".ONNX":
                case ".onnx":
                    return true;
                default:
                    return false;
            }
        } else if(uploadType.equals("requirement")) {
            switch (suffix) {
                case ".txt":
                    return true;
                default:
                    return false;
            }
        } else if(uploadType.equals("spectification")) {
            switch(suffix) {
                case ".pdf":
                    return true;
                case ".doc":
                    return true;
                default:
                    return false;
            }
        }
        else if(uploadType.equals("dataset")) {
            switch(suffix) {
                case ".zip":
                case ".rar":
                case ".7z":
                    return true;
                default:
                    return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 为上传文件计算字节数组的MD5值，并保存在数据库中，防止文件重复上传
     */
    public String checkMd5(Chunk chunk) {
        // select
        return "";
    }

}
